var express = require('express');
var router = express.Router();

// 设置token按1/s 面对面建群按ms 有效期
router.post('/setconfig', function(req, res, next) {
	let configItem = {
		password: "",
		username: "",
		tokenExp: 60 * 60, //默认1小时,
		faceTofaceExp: 1000 * 30, //默认30秒
		OperationRate: 1000 * 5 ,//清数据 执行速率5s
		positionNumber,//经纬度 精确值位数 面对面
		MaximumNumberOfPeople,//最大建群人数
	}
	configItem = Object.assign({}, req.body);
	let obj = {
		code: 405, // 信息错误 禁止
		message: "验证失败，禁止写入"
	}
	if (configItem.password != "shsimshsim" || configItem.username != "shsim") {
		res.send(JSON.stringify(obj));
		return;
	}
	if (configItem.MaximumNumberOfPeople<2||configItem.tokenExp <= 0 || configItem.faceTofaceExp <= 0 || configItem.OperationRate <= 0 || configItem.positionNumber<0) {
		obj.message="非法数据，配置失败";
		res.send(JSON.stringify(obj));
		return;
	}
	expiresIn = configItem.tokenExp;
	faceToFaceFailureTime = configItem.faceTofaceExp;
	OperationRate = configItem.OperationRate;
	MaximumNumberOfPeople=configItem.MaximumNumberOfPeople?configItem.MaximumNumberOfPeople:MaximumNumberOfPeople
	positionNumber=configItem.positionNumber
	if(OperationTemp){
		clearInterval(OperationTemp);
	}
	OperationfaceToFaceTime();//重新执行定时器 监听群聊信息
	OperationfaceToFaceTimeV2();//重新执行定时器 监听群聊信息
	obj = {
		code: 200,
		message: "配置成功"
	}
	res.send(JSON.stringify(obj));
})
//获取配置信息
router.post('/getconfig', function(req, res, next) {
	let obj = {
		code: 405, // 信息错误 禁止
		message: "验证失败，禁止获取"
	}
	try {
		let configItem = {
			code: 200,
			message: "配置信息",
			tokenExp: expiresIn,
			faceTofaceExp: faceToFaceFailureTime,
			OperationRate,
			MaximumNumberOfPeople,
			positionNumber,
		}
		let password = "";
		let username = "";

		// password = req.body.password;
		// username = req.body.username;
		({password,username}=req.body);
		if (password != "shsimshsim" || username != "shsim") {
			res.send(JSON.stringify(obj));
			return;
		}
		res.send(JSON.stringify(configItem));
	} catch (e) {
		//TODO handle the exception
		res.send(JSON.stringify(e));
	}

})
module.exports = router;